package com.xuyiwei.xblog.modular.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.xuyiwei.xblog.business.system.model.Dict;
import com.xuyiwei.xblog.business.system.model.Link;
import com.xuyiwei.xblog.business.system.service.ILinkService;
import com.xuyiwei.xblog.core.base.constant.enums.CommonStatusEnum;
import com.xuyiwei.xblog.core.base.controller.BaseController;
import com.xuyiwei.xblog.core.base.response.Result;
import com.xuyiwei.xblog.core.exception.BizExceptionEnum;
import com.xuyiwei.xblog.core.exception.GunsException;
import com.xuyiwei.xblog.core.util.ToolUtil;
import com.xuyiwei.xblog.modular.system.warpper.LinkWarpper;
import com.xuyiwei.xblog.modular.system.warpper.UserWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.xuyiwei.xblog.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 友情赞助控制器
 *
 * @author fengshuonan
 * @Date 2018-12-02 16:33:36
 */
@Controller
@RequestMapping("/link")
public class LinkController extends BaseController {

    private String PREFIX = "/system/link/";

    @Autowired
    private ILinkService linkService;

    /**
     * 跳转到友情赞助首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "link.html";
    }

    /**
     * 跳转到添加友情赞助
     */
    @RequestMapping("/link_add")
    public String linkAdd() {
        return PREFIX + "link_add.html";
    }

    /**
     * 跳转到修改友情赞助
     */
    @RequestMapping("/link_update/{linkId}")
    public String linkUpdate(@PathVariable Integer linkId, Model model) {
        Link link = linkService.selectById(linkId);
        model.addAttribute("item",link);
        LogObjectHolder.me().set(link);
        return PREFIX + "link_edit.html";
    }

    /**
     * 获取友情赞助列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Result<List<Map<String, Object>>> list(String condition) {
        Result<List<Map<String, Object>>> result = Result.instance();
        try{
            Wrapper<Link> wrapper = new EntityWrapper<>();
            wrapper.where(" status != 0 ");
            List<Link> list = linkService.selectList(wrapper);
            result.setData((List<Map<String, Object>>)new LinkWarpper(list).warp());
        }catch (Exception e){
            result.setError("500","获取文章标签出错");
        }
        return result;
    }

    /**
     * 新增友情赞助
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Result<Boolean> add(Link link) {
        Result<Boolean> result = Result.instance();
        try{
            link.setCreateTime(new Date());
            link.setStatus(CommonStatusEnum.USE.getKey());
            linkService.insert(link);
            result.setData(true);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 删除友情赞助
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(required = true, value = "ids[]") List<Integer> ids) {
        Result<Boolean> result = Result.instance();
        try{
            if (ToolUtil.isEmpty(ids)) {
                throw new GunsException(BizExceptionEnum.REQUEST_NULL);
            }
            this.linkService.deleteBatchIds(ids);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 修改友情赞助
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Result<Boolean> update(Link link) {
        Result<Boolean> result = Result.instance();
        try{
            link.setUpdateTime(new Date());
            linkService.updateById(link);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 友情赞助详情
     */
    @RequestMapping(value = "/detail/{linkId}")
    @ResponseBody
    public Result<Link> detail(@PathVariable("linkId") Integer linkId) {
        Result<Link> result = Result.instance();
        try{
            Link link = linkService.selectById(linkId);
            result.setData(link);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }
}
